/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ysw.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ysw.dao.SendDao;
import ysw.databaseconnection.DBConn;
import ysw.vo.Send;

/**
 *
 * @author Administrator
 */
public class SendJdbc implements SendDao {
     private  Connection conn=null;
    private PreparedStatement psmt=null;
    private ResultSet rs=null;
    public SendJdbc(){}
    public Connection getConn(){
        try{
            if(this.conn==null||this.conn.isClosed()){
                DBConn mc=new DBConn();
                this.conn=mc.getConn();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

    public Send addSend(Send send) {
      String sql="insert into sends(eid,ename,ephone,carnumber,stime,finish) values(?,?,?,?,?,?)";
        try{
     
            psmt=this.getConn().prepareStatement(sql);
            
            psmt.setInt(1,send.getEid());   
             psmt.setString(2,send.getEname());  
       
             psmt.setString(3,send.getEphone());

             psmt.setString(4,send.getCarnumber());
             psmt.setDate(5,send.getStime());
             psmt.setByte(6,send.getFinish());
         
       
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return send;
    }

    
    public List showSend() throws SQLException {
       String sql="select * from sends";
       
        List sendList=new ArrayList();
    try{
        psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
            Send send=new Send();
            send.setId(rs.getInt("id"));
            send.setEid(rs.getInt("eid"));
            send.setEname(rs.getString("ename"));
           
            send.setEphone(rs.getString("ephone"));
              send.setCarnumber(rs.getString("carnumber"));
             send.setStime(rs.getDate("stime"));
          
            send.setFinish(rs.getByte("finish")); 
            
            sendList.add(send);
     
  
        }
        return sendList;
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
             if(rs!=null){
                   rs.close();
                   rs=null;
                }
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return sendList;
    }
public Send showOneSend(Integer id){
     ResultSet rs=null;
    String sql="select * from sends where id="+id;
   Send send=new Send();
    try{
      psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
            send.setId(rs.getInt("id"));
            send.setEid(rs.getInt("eid"));
            send.setEname(rs.getString("ename"));
           
            send.setEphone(rs.getString("ephone"));
             send.setCarnumber(rs.getString("carnumber"));
             send.setStime(rs.getDate("stime"));
            send.setFinish(rs.getByte("finish")); 
        }
 
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
            psmt.close();
              
            }catch(SQLException e){
                        e.printStackTrace();
                        }
        try{conn.close();
        
        }catch(SQLException e){
             e.printStackTrace();
        }
            }
            return send;
}


 public Send updateSaveSend(Send send){
          String sql="update sends set id=?,eid=?,ename=?,ephone=?,carnumber=?,stime=?,finish=? where id="+send.getId();
     try{
            
          psmt=this.getConn().prepareStatement(sql);
          
             psmt.setInt(1,send.getId());
             psmt.setInt(2,send.getEid());   
             psmt.setString(3,send.getEname());  
       
             psmt.setString(4,send.getEphone());
                  
             psmt.setString(5,send.getCarnumber());
              psmt.setDate(6,send.getStime());
             psmt.setByte(7,send.getFinish());
          
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
         try{
             psmt.close();
            
         }catch(SQLException e){
             e.printStackTrace();
         }
     }
     return send;
} 
}
